package SURF.util;

import ij.IJ;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;

// TODO move IJ-related code into IJFacade

/** Interest Point class. */
public class InterestPoint {

	/** Interpolated X-coordinate. */
	public float x;
	
	/** Interpolated Y-coordinate. */
	public float y;

	/** Value of the hessian determinant (blob response) means the strength of the interest point. */
	public float strength;
	
	/** Trace of the hessian determinant. */
	float trace;
	
	/** Sign of hessian traces (laplacian sign).<br>
	 * <code>true</code> means >= 0, <code>false</code> means < 0.
	 * (Signs are saved separately for better matching performance.) */
	public float sign;
	
	/** Detected scale. */
	public float scale;
	
	/** Orientation measured anti-clockwise from +ve x-axis. The default is 0 (i.e. upright SURF). */
	public float orientation;

	/** Vector of descriptor components. */
	public float[] descriptor;

	public InterestPoint() {}
	
	public InterestPoint(float x, float y, float strength, float trace, float scale) {
		this.x = x;
		this.y = y;
		this.strength = strength;
		this.trace = trace;
		this.scale = scale;
		this.sign = (trace >= 0) ? 1 : -1;
	}


	public String toStringSurf() {
		StringBuilder sb = new StringBuilder();
		Formatter f = new Formatter(sb, Locale.US); // (all output will be sent to sb)		
                f.format("%f %f %f %f %f %f \n", x, y, orientation, scale, strength, trace );
                int descSize = (descriptor == null) ? 0 : descriptor.length;			
		if (descSize > 0) {
			for (int i = 0; i < descSize; i++) {
				f.format("%f ", descriptor[i]);
			}			
		}
		return f.toString();
	}

	public static void SurfSaveToFile(List<InterestPoint> ipts, String fileName)
        {
		try {                    
                    PrintWriter out = new PrintWriter(fileName);
			if (ipts != null && ipts.size() > 0) {
				
				// Header                                
                                out.println(ipts.size()); // Number of Interest Points
                                // Points
				for (InterestPoint ipt : ipts)
                                    out.println(ipt.toStringSurf());

                                /*for(int i=0; i<ipts.size(); i++)
                                    out.println(ipts.get(i));*/                                    
			}
                        else
                        {
                        out.println(ipts.size());
                        }
			out.close();
		} catch (FileNotFoundException e) {
			IJ.error("SURF: savePointsToFile", e.getMessage());
		}
		
	}






        
        

	
}
